package com.ruicar.afs.cloud.channel.common.mq.sender.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsRelTemp;
import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsTemp;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsRelTempService;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsTempService;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 进件放款提交案件审批实现类
 *
 * @author panhj
 */
@Service
@AllArgsConstructor
public class CaseAffiliationRelSenderImpl {
    /***
     * 挂靠单位service
     */
    private final ChannelAffiliatedUnitsTempService channelAffiliatedUnitsTempService;
    /**
     * 挂靠关联合作商service
     */
    private final ChannelAffiliatedUnitsRelTempService channelAffiliatedUnitsRelTempService;


    /**
     * 返回
     *
     * @param id
     * @return
     */
    public AfsTransEntity<AffiliationRelInfo> returnAffiliationRelInfo(String id) {
        AfsTransEntity<AffiliationRelInfo> transEntity = new AfsTransEntity<>();
        AffiliationRelInfo affiliationRelInfo = new AffiliationRelInfo();
        AffiliationDto affiliationDto = this.affiliationDto(id);
        ArrayList<AffiliationRelDto> affiliationRelDtoArrayList = this.affiliationRelDtoArrayList(id);
        affiliationRelInfo.setAffiliationDto(affiliationDto);
        affiliationRelInfo.setAffiliationRelDtoArrayList(affiliationRelDtoArrayList);
        transEntity.setTransCode(MqTransCode.AFS_CHANNEL_CHANNEL_CASE_CTM_SAVE_AFFILIATION);
        transEntity.setData(affiliationRelInfo);
        return transEntity;
    }

    /**
     * 保存的挂靠信息
     */
    public AffiliationDto affiliationDto(String id) {
        AffiliationDto affiliationDto = new AffiliationDto();
        ChannelAffiliatedUnitsTemp channel = channelAffiliatedUnitsTempService.getById(id);
        affiliationDto.setAffiliatedName(channel.getAffiliatedName());
        affiliationDto.setApplyTime(channel.getApplyTime());
        affiliationDto.setBusiType(channel.getBusiType());
        affiliationDto.setCarCardComplete(channel.getCarCardComplete());
        affiliationDto.setCooperPlat(channel.getCooperPlat());
        affiliationDto.setCreateBy(channel.getCreateBy());
        affiliationDto.setCreateTime(channel.getCreateTime());
        affiliationDto.setInLibraryOrNot(channel.getInLibraryOrNot());
        affiliationDto.setLegalPersonIdcard(channel.getLegalPersonIdcard());
        affiliationDto.setLegalPersonName(channel.getLegalPersonName());
        affiliationDto.setLegalPersonTel(channel.getLegalPersonTel());
        affiliationDto.setStatus(channel.getStatus());
        affiliationDto.setOperateStatus(channel.getOperateStatus());
        affiliationDto.setOperCity(channel.getOperCity());
        affiliationDto.setRegisteredAddress(channel.getRegisteredAddress());
        affiliationDto.setRegisteredCity(channel.getRegisteredCity());
        affiliationDto.setRegisteredProvince(channel.getRegisteredProvince());
        affiliationDto.setRegisteredCapital(channel.getRegisteredCapital());
        affiliationDto.setRegisteredTime(channel.getRegisteredTime());
        affiliationDto.setSocUniCrtCode(channel.getSocUniCrtCode());
        affiliationDto.setWhetherLicence(channel.getWhetherLicence());
        affiliationDto.setDelFlag(channel.getDelFlag());
        affiliationDto.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
        affiliationDto.setMonthlySurplusNum(channel.getMonthlySurplusNum());
        affiliationDto.setTotalApplicationNum(channel.getTotalApplicationNum());
        affiliationDto.setTotalSurplusNum(channel.getTotalSurplusNum());
        affiliationDto.setLongTermOrNot(channel.getLongTermOrNot());
        affiliationDto.setId(channel.getId());
        affiliationDto.setUpdateTime(channel.getUpdateTime());
        affiliationDto.setUpdateBy(channel.getUpdateBy());
        affiliationDto.setLimitValue(channel.getLimitValue());
        affiliationDto.setWarningValue(channel.getWarningValue());
        affiliationDto.setFoundedTime(channel.getFoundedTime());
        affiliationDto.setTermValidity(channel.getTermValidity());
        affiliationDto.setOprTermValidity(channel.getOprTermValidity());
        affiliationDto.setTransPermitValidity(channel.getTransPermitValidity());
        affiliationDto.setPeopleCardComplete(channel.getPeopleCardComplete());
        affiliationDto.setPeoCarCardProportion(channel.getPeoCarCardProportion());
        affiliationDto.setPeoCarCardComplete(channel.getPeoCarCardComplete());
        affiliationDto.setOperateVehicles(channel.getOperateVehicles());
        affiliationDto.setOperationYears(channel.getOperationYears());
        return affiliationDto;
    }

    /**
     * 保存挂靠关联的合作商信息
     */
    public ArrayList<AffiliationRelDto> affiliationRelDtoArrayList(String id) {
        ArrayList<AffiliationRelDto> affiliationRelDtoArrayList = new ArrayList<>();
        ChannelAffiliatedUnitsTemp channel = channelAffiliatedUnitsTempService.getById(id);
        List<ChannelAffiliatedUnitsRelTemp> list = this.channelAffiliatedUnitsRelTempService.list(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId()));
        for (ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp : list) {
            AffiliationRelDto affiliationRelDto = new AffiliationRelDto();
            affiliationRelDto.setArriliatedId(channelAffiliatedUnitsRelTemp.getArriliatedId());
            affiliationRelDto.setChannelId(channelAffiliatedUnitsRelTemp.getChannelId());
            affiliationRelDto.setCreateBy(channelAffiliatedUnitsRelTemp.getCreateBy());
            affiliationRelDto.setCreateTime(channelAffiliatedUnitsRelTemp.getCreateTime());
            affiliationRelDto.setDelFlag(channelAffiliatedUnitsRelTemp.getDelFlag());
            affiliationRelDto.setId(channelAffiliatedUnitsRelTemp.getId());
            affiliationRelDto.setStatus(channelAffiliatedUnitsRelTemp.getStatus());
            affiliationRelDto.setUpdateBy(channelAffiliatedUnitsRelTemp.getUpdateBy());
            affiliationRelDto.setUpdateTime(channelAffiliatedUnitsRelTemp.getUpdateTime());
            affiliationRelDto.setChannelTermValidity(channelAffiliatedUnitsRelTemp.getChannelTermValidity());
            affiliationRelDtoArrayList.add(affiliationRelDto);
        }
        return affiliationRelDtoArrayList;
    }
}
